package com.smartjobs.action;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;
import com.smartjobs.bean.JobSeekerVO;
import com.smartjobs.loader.MergeFile;
import com.smartjobs.loader.SwoopTest;
import com.smartjobs.reasoner.ReasonerQuery;
import com.smartjobs.util.RuntimeSettings;

@SuppressWarnings("serial")
public class JobSeekersAction extends ActionSupport {

	ArrayList<JobSeekerVO> jobseekers = new ArrayList<JobSeekerVO>();
	String skill = "";
	String refresh = "";

	public String getRefresh() {
		return refresh;
	}

	public void setRefresh(String refresh) {
		this.refresh = refresh;
	}

	public String getSkill() {
		return skill;
	}

	public void setSkill(String skill) {
		this.skill = skill;
	}

	public ArrayList<JobSeekerVO> getJobseekers() {
		return jobseekers;
	}

	public void setJobseekers(ArrayList<JobSeekerVO> jobseekers) {
		this.jobseekers = jobseekers;
	}

	public String execute() {
		int i;
		ArrayList<JobSeekerVO> jobSeekerList = new ArrayList<JobSeekerVO>();

		if (!refresh.equals("")) {
			System.out.println("inside refresh");
			try {
				saveUrl(RuntimeSettings.location + "/"
						+ RuntimeSettings.DDSFilename, RuntimeSettings.DDSUrl
						+ "/" + RuntimeSettings.DDSFilename);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			refresh = "";

			new SwoopTest().addSemantics(RuntimeSettings.location
					+ "/smartJobs.rdf");
			MergeFile.mergeFile();
		}

		//RDFLoader.loadFromDataBase();
		jobSeekerList = new ReasonerQuery().getAllJobSeekers();

		for (i = 0; i < jobSeekerList.size(); i++) {

			if (jobSeekerList.get(i).getSource()
					.equalsIgnoreCase("www.smartJobs.com")) {

				jobSeekerList.get(i).setSourceFlag(1);
				System.out.println("source: "
						+ jobSeekerList.get(i).getSource()
						+ " setting source flag to 1");

			} else {

				jobSeekerList.get(i).setSourceFlag(0);

			}
		}

		if (skill.equalsIgnoreCase("")) {

			jobseekers = jobSeekerList;

		} else {

			for (i = 0; i < jobSeekerList.size(); i++) {

				if (jobSeekerList.get(i).getSkills() != null) {

					if (jobSeekerList.get(i).getSkills().toLowerCase()
							.contains(skill.toLowerCase())) {

						jobseekers.add(jobSeekerList.get(i));

					}
				}
			}
		}
		return SUCCESS;
	}

	public String init() {
		return SUCCESS;

	}

	public void saveUrl(String filename, String urlString)
			throws MalformedURLException, IOException {
		BufferedInputStream in = null;
		FileOutputStream fout = null;
		URLConnection uc;
		try {

			URL urlc = new URL(urlString);
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
					"192.16.3.254", 8080));
			uc = (URLConnection) urlc.openConnection(proxy);
			uc.addRequestProperty("User-Agent",
					"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2");
			uc.connect();

			in = new BufferedInputStream(uc.getInputStream());
			fout = new FileOutputStream(filename);

			byte data[] = new byte[1024];
			int count;
			while ((count = in.read(data, 0, 1024)) != -1) {
				fout.write(data, 0, count);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (in != null)
				in.close();
			if (fout != null)
				fout.close();
		}
	}

}
